<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台签到ajax
 * @package  ajax
 * @subpackage  shop
 * @author   allen.qiang
 */

quanli_require('module/base/promotion/signIn.class.php');

class ajax_checkIn {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //保存用户签到
                    $this->save();
                    break;

                case 2: //查询签到信息
                    $this->search();
                    break;

                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    //保存用户签到
    private function save() {

        global $G_SHOP;

        //实例签到类
        $signIn = new signIn( $G_SHOP->DBCA );

        //调用保存用户签到方法
        $result_array = $signIn->saveCheckIn( $G_SHOP->currentPCustID, $_REQUEST['promotionID'] ,$G_SHOP->EAI );

        echo json_encode( $result_array );

    }

    /**
     * 查询签到信息
     */
    function search(){

        global $G_SHOP;

        //实例签到类
        $signIn = new signIn( $G_SHOP->DBCA );

        //接收参数
        $params_month = $_REQUEST['month'];
        $pcustID = $G_SHOP->currentPCustID;
        $promotionID = $_REQUEST['promotionID'];

        //返回数组
        $return_array = array();

        $week = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');

        //获取今年
        $year = date('Y');

        //本月份
        $current_month = intval( date('m') );

        if( $params_month >0 && $params_month <10 )
            $month = '0'.$params_month;
        else
            $month = $params_month;

        //当月第一天
        $firstDay = $year.'-'.$month.'-01';

        if ( $params_month < $current_month ) {

            //获取本月最后一天
            $lastDay = date('Y-m-t',strtotime($firstDay));

        } elseif( $params_month == $current_month ) {

            $lastDay = date('Y-m-d');

        } else {

            $this->toReturn('ok', 200, $return_array);

        }

        for( $date =$firstDay; $date <=$lastDay; $date++ ){

            $a_week = date('w' , strtotime($date) );

            $b_week = $week[$a_week];

            //循环查询每一天签到信息
            $arg_array = array(
                'promotionID' => $promotionID,
                'pcustID' => $pcustID,
                'date' => $date
            );
            $sign_in_log_array = $signIn->searchSignInLog( $arg_array );

            $return_array[$date] = array(
                'pcustID' => $pcustID,
                'checkIn' => $sign_in_log_array ? '已签到' : '<font style="color: red;">未签到</font>',
                'remark' => $sign_in_log_array ? $sign_in_log_array[0]['remark'] : 0,
                'points' => $sign_in_log_array ? $sign_in_log_array[0]['points'] : 0,
                'week' => $b_week,
                'date' => date('m月d日' , strtotime($date))
            );

        }

        //将数组按降序排列
        krsort($return_array);

        $this->toReturn( 'ok', 200, $return_array );

    }

}